package com.ace.token.api.exception;

import com.ace.token.common.bean.ResponseDTO;
import com.ace.token.common.constant.C;
import com.ace.token.common.constant.R;
import com.ace.token.config.locale.RLocale;
import com.ace.token.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 错误控制器,负责处理不同的错误
 * @Author
 * @Date 2018年07月02日 下午5:44
 */
@RestControllerAdvice
public class ControllerException {

    @Autowired
    private RLocale rLocale;

    /**
     * 请求参数验证不通过将调用这个方法
     * 作用@RequestBody()
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseDTO constraintViolationException(MethodArgumentNotValidException exception){
//        return R.COMM_ERR_INVALID_PARA;
        return rLocale.getDTO("COMM_ERR_INVALID_PARA", C.T_C_3);
    }
    /**
     * 请求参数验证不通过将调用这个方法
     * 作用@RequestParam()
     * @param exception
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseDTO missingServletRequestParameterException(MissingServletRequestParameterException exception){
//        return R.COMM_ERR_INVALID_PARA;
        return rLocale.getDTO("COMM_ERR_INVALID_PARA", C.T_C_3);
    }

    /**
     * 请求参数枚举类型验证不通过将调用这个方法
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResponseDTO methodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception){
//        return R.COMM_ERR_INVALID_PARA;
        return rLocale.getDTO("COMM_ERR_INVALID_PARA", C.T_C_3);
    }

    /**
     * 请求参数验证不通过将调用这个方法
     * 作用@RequestParam()数据为空的情况
     * @param exception
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseDTO httpMessageNotReadableException(HttpMessageNotReadableException exception){
//        return R.COMM_ERR_INVALID_PARA;
        return rLocale.getDTO("COMM_ERR_INVALID_PARA", C.T_C_3);
    }


    /**
     * 执行业务出现异常调用这个方法
     * @param exception
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseDTO businessException(BusinessException exception){
        return exception.getResponse();
    }


    /**
     * 404错误调用这个方法
     * @param exception
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(code = HttpStatus.NOT_FOUND)
    public void noHandlerFoundException(NoHandlerFoundException exception){

    }


    /**
     * 请求方式错误
     * @param exception
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(code = HttpStatus.METHOD_NOT_ALLOWED)
    public void httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception){

    }


    /**
     * 其他错误调用这个方法
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public void exception(Exception exception){
          exception.printStackTrace();
    }
}
